Imports System.Collections.Generic
Imports System.Linq
Imports System.Web

#Region "using"
Imports System.Data
Imports QUANLY.DTO
#End Region
Public Class KHACHHANGDAO
	Inherits AbstractDAO
	Public Sub New(strPath As [String])
		MyBase.New(strPath)
	End Sub

	Public Function SelectAll() As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_KHACHHANG_SelectAll"
			MyBase.FillDataSet(ds, "KHACHHANG")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

	Public Function SelectByID(id As Integer) As DataSet
		Dim ds As DataSet
		Try
			ds = New DataSet()
			MyBase.sql = "usp_KHACHHANG_SelectByID"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, id)
			MyBase.FillDataSet(ds, "KHACHHANG")
			Return ds
		Catch e As System.Exception
			Throw New Exception(e.Message, e.InnerException)
		Finally
			MyBase.CleanUpObject()
		End Try
	End Function

    Public Function Search(ByVal id As String, ByVal name As String) As DataSet
        Dim ds As DataSet
        Try
            ds = New DataSet()
            MyBase.sql = "SELECT ID, TenKH, Gioitinh, CMND, DiaChi, SoDT, QuocTich " & _
                        "FROM KHACHHANG " & _
                        "WHERE ID = " & IIf(id = "", "ID", id) & " And " & _
                            "UPPER(TenKH) like " & IIf(name = "", "UPPER(TenKH)", "N'%" & name.Trim.ToUpper() & "%'")
            MyBase.InitializeCommand()
            MyBase.FillDataSet(ds, "KHACHHANG")
            Return ds
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        Finally
            MyBase.CleanUpObject()
        End Try
    End Function

	Public Function Insert(obj As KHACHHANGDTO) As Boolean
		Try
			MyBase.sql = "usp_KHACHHANG_Insert"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@TenKH", SqlDbType.NVarChar, 1000, obj.TenKH)
			MyBase.AddParameter("@Gioitinh", SqlDbType.Bit, 1000, obj.Gioitinh)
			MyBase.AddParameter("@CMND", SqlDbType.NVarChar, 1000, obj.CMND)
			MyBase.AddParameter("@DiaChi", SqlDbType.NVarChar, 1000, obj.DiaChi)
			MyBase.AddParameter("@SoDT", SqlDbType.NVarChar, 1000, obj.SoDT)
			MyBase.AddParameter("@QuocTich", SqlDbType.NVarChar, 1000, obj.QuocTich)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

	Public Function Update(obj As KHACHHANGDTO) As Boolean
		Try
			MyBase.sql = "usp_KHACHHANG_Update"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID)
			MyBase.AddParameter("@TenKH", SqlDbType.NVarChar, 1000, obj.TenKH)
			MyBase.AddParameter("@Gioitinh", SqlDbType.Bit, 1000, obj.Gioitinh)
			MyBase.AddParameter("@CMND", SqlDbType.NVarChar, 1000, obj.CMND)
			MyBase.AddParameter("@DiaChi", SqlDbType.NVarChar, 1000, obj.DiaChi)
			MyBase.AddParameter("@SoDT", SqlDbType.NVarChar, 1000, obj.SoDT)
			MyBase.AddParameter("@QuocTich", SqlDbType.NVarChar, 1000, obj.QuocTich)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

	Public Function Delete(id As Integer) As Boolean
		Try
			MyBase.sql = "usp_KHACHHANG_Delete"
			MyBase.InitializeCommand()
			MyBase.AddParameter("@ID", SqlDbType.Int, 1000, id)
			Return ExecuteStoredProcedure()
		Catch
			Return False
		End Try
	End Function

End Class
